package com.halopay.controler;

import com.halopay.entity.Packet;
import com.halopay.entity.ScheduleJobEntity;
import com.halopay.service.PacketService;
import com.halopay.common.utils.PageUtils;
import com.halopay.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;


/**
 * @author zx
 * @email zdkk@foxmail.com
 * @date 2019-01-18 17:13:28
 */
@RestController
@RequestMapping("api/packet")
public class PacketController {
    @Autowired
    private PacketService packetService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = packetService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        Packet packet = packetService.selectById(id);

        return R.ok().put("packet", packet);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Valid @RequestBody Packet packet) {
        packetService.insert(packet);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody Packet packet) {
        packetService.updateById(packet);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        packetService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 创建一个发红包任务
     */
    @RequestMapping("/create")
    public R create(@RequestBody Packet packet,@RequestBody ScheduleJobEntity scheduleJobEntity) {
        packetService.createPacket(scheduleJobEntity,packet);
        return R.ok();
    }

    /**
     * 抢一个发红包
     */
    @RequestMapping("/robPacket")
    public R robPacket(@RequestBody Packet packet,@RequestBody ScheduleJobEntity scheduleJobEntity) {
        packetService.createPacket(scheduleJobEntity,packet);
        return R.ok();
    }
}
